package com.googlecode.yonggemovie.ui;

import java.awt.Color;
import java.awt.Font;
import java.util.Random;

import com.googlecode.yonggemovie.widgets.splash.JSplash;

/**
 * 启动画面设置
 * @author Kobe Su,2009-12-8
 */
public class SplashSetting {

	private static final String SPLASH_FILE_PATH = "/com/googlecode/yonggemovie/resources/images/splash.jpg";
	private static boolean displayProgress = true; //是否显示进度条
	private static boolean displayMessages = true; //是否在进度条上显示自定义文字
	private static boolean displayPercent = false; //是否在进度条上显示百分比（与文字只能二者选一）
	private static final String VERSION_STRING = "v0.10 build 01";
	
	
	private SplashSetting(){}
	
	public static void init() {
		String[] loadingMessages = {
				null, // at first, don't display a message
				"Initializing", "Loading libraries", "Applying configuration",
				"Setting up logger", "Applying license", "Connecting to db",
				"Quering customers", "Quering products & catalogs",
				"Starting up GUI", null // at the end don't display a message,keep last message longer on display
		};

		//预加载启动画面
		JSplash m_splash = new JSplash(SplashSetting.class.getResource(SPLASH_FILE_PATH),
				displayProgress,displayMessages,displayPercent,
				VERSION_STRING,new Font("Tahoma", 0, 11),Color.RED);
		
		//显示启动画面
		m_splash.splashOn();

		//设置进度条的显示交互，以下为模拟效果，实际情况，可能需要在某些位置用实际的方法代替（比如初始化数据库等)
		//进度条在初始化JSplash时已经初始化，最大值100，最小值0，初始值0
		//下面设置进度条每次的前进进度为10，但每次间隔时间（此处用线程睡眠来模拟，实际情况可以调用实际方法做事）为1秒内随机行为，所以总时间不超过10秒
		Random r = new Random();
		for (int i = 0, j = 0; i <= 100; i += 10, j++) {
			int millis = (r.nextInt(10) + 1) * 100;

			if (displayMessages && !displayPercent) {
				m_splash.setProgress(i, loadingMessages[j]);
			} else {
				m_splash.setProgress(i);
			}

			try {
				Thread.sleep(millis);
			} catch (Exception e) {
				// do nothing
			}
		}

		// 给用户1秒钟时间看到进度条完成100%后的整个启动画面
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		}

		//隐藏启动画面并且销毁
		m_splash.splashOff();
	}
	
	
	
	public static void main(String[] args) {
		

	}

}
